import sys,traceback,Ice
import mysql.connector
Ice.loadSlice("Printer.ice")
import QuerySystem

class QueryResultsI(QuerySystem.QueryResults):
    def __init__(self, name):
        self.name = name
    def QueryByNo(self,s,current=None):
        print(self.name+" "+s)
        #mark_dict = {"English":100,"Math":100}
        #return dict
		
        try:
            cnx = mysql.connector.connect(host='127.0.0.1', user='root', password='19911020', database='test')
            cursor = cnx.cursor()
        except mysql.connector.Error as err:
            print(err)
			#exit()
        sql = "select course_title,course_score from StudentScore where userid = "+s+";"
        cursor.execute(sql)
        res = cursor.fetchall()
        mark_dict = {}
        for Temp in res:
            mark_dict[Temp[0]] = Temp[1]
        cursor.close()
        cnx.close()
        return mark_dict
		
class Server(Ice.Application):
    def run(self, args):
        if len(args) > 1:
            print(self.appName() + ": too many arguments")
            return 1

        properties = self.communicator().getProperties()
        adapter = self.communicator().createObjectAdapter("QueryResults")
        id = self.communicator().stringToIdentity(properties.getProperty("Identity"))
        adapter.add(QueryResultsI(properties.getProperty("Ice.ProgramName")), id)
        adapter.activate()
        self.communicator().waitForShutdown()
        return 0

app = Server()
sys.exit(app.main(sys.argv))
